package com.fading.mall.product.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fading.mall.product.entity.CategoryEntity;
import com.fading.mall.product.service.CategoryService;
import com.fading.mall.product.vo.Catelog2Vo;
import io.lettuce.core.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisClient;

    @GetMapping("/get/redisson")
    @ResponseBody
    public String getRedisson() {
        RLock lock = redisClient.getLock("son-lock");
        System.out.println(lock);
        return lock.toString();
    }

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {

        //1、查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categories", categoryEntities);
        return "index";
    }


    //index/json/catalog.json
    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }


    @GetMapping("/read")
    @ResponseBody
    public String read() throws InterruptedException {
        RReadWriteLock readWriteLock = redisClient.getReadWriteLock("rw-lock");
        String read = "read";
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        try {
            log.debug("info{}", read);
            Thread.sleep(10000);
        } finally {
            rLock.unlock();
        }
        return read;
    }

    @GetMapping("/write")
    @ResponseBody
    public String write() throws InterruptedException {
        RReadWriteLock readWriteLock = redisClient.getReadWriteLock("rw-lock");
        String write = "write";
        RLock wLock = readWriteLock.writeLock();
        wLock.lock();
        try {
            log.debug("info{}", write);
            Thread.sleep(10000);
        } finally {
            wLock.unlock();
        }
        return write;
    }


    @GetMapping("/close")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisClient.getCountDownLatch("door");
        door.trySetCount(4);
        door.await();
        return "关门";
    }

    @GetMapping("/out/{id}")
    @ResponseBody
    public String out(@PathVariable("id") Long id){
        RCountDownLatch door = redisClient.getCountDownLatch("door");
        door.countDown();
        return id + "out";
    }


}
